﻿using Microsoft.Xna.Framework;
using NuclearWinter.GameFlow;
using StoCombatLogHUD.Toolkit.ExtensionMethods;
using NuclearUI = NuclearWinter.UI;

namespace StoCombatLogHUD.GameStates
{
	/// <summary>
	/// 
	/// </summary>
	public class ExceptionState : GameStateFadeTransition<Main>
	{
		/// <summary>
		/// Gets or sets the display panel.
		/// </summary>
		/// <value>
		/// The display panel.
		/// </value>
		private NuclearUI.Panel DisplayPanel { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="ExceptionState"/> class.
		/// </summary>
		/// <param name="game">The game.</param>
		/// <param name="exception">The exception.</param>
		public ExceptionState(Main game, string exception)
			: base(game)
		{
			var title = new NuclearUI.Label(Game.MainScreen, "Ooops, an error occured!\nSorry for the inconvenience...")
				{
					Font = Game.UIStyle.ExtraLargeFont,
					Color = Color.DarkRed
				};

			var exceptionText = new NuclearUI.TextArea(game.MainScreen)
				{
					Font = Game.UIStyle.MediumFont,
					IsReadOnly = true
				}
				.Perform(textArea => textArea.ReplaceContent(exception));

			DisplayPanel = new NuclearUI.Panel(Game.MainScreen, Game.UIStyle.Panel, Game.UIStyle.PanelCornerSize)
				.Perform(
					mainPanel =>
					mainPanel.AddChild(
						new NuclearUI.BoxGroup(Game.MainScreen, NuclearUI.Orientation.Vertical, 0, NuclearUI.Anchor.Start)
							{
								AnchoredRect = NuclearUI.AnchoredRect.CreateFull(5)
							}
							.Perform(
								group =>
									{
										group.AddChild(
											title
												.Perform(
													widget => widget.AnchoredRect = NuclearUI.AnchoredRect.CreateTopLeftAnchored(0, -20, Game.MainScreen.Width - 40, 120)));
										group.AddChild(
											exceptionText
												.Perform(
													widget => widget.AnchoredRect = NuclearUI.AnchoredRect.CreateTopLeftAnchored(0, 0, Game.MainScreen.Width - 40, 320)));
									})));
		}

		/// <summary>
		/// Starts this instance.
		/// </summary>
		public override void Start()
		{
			Game.SetMainScreenContent(DisplayPanel);
			base.Start();
		}

		/// <summary>
		/// Updates this instance.
		/// </summary>
		/// <param name="elapsedTime">The elapsed time.</param>
		public override void Update(float elapsedTime)
		{
			DisplayPanel.Update(elapsedTime);
			DisplayPanel.DoLayout(DisplayPanel.LayoutRect);
		}

		/// <summary>
		/// Draws this instance.
		/// </summary>
		public override void Draw()
		{
			// Nothing todo here
		}
	}
}
